package jp.sourceforge.akjrcp.ui.param;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import jp.sourceforge.akjrcp.commons.ui.widget.LayoutBuilder;

import org.apache.commons.lang.math.NumberUtils;

public class BitrateChangeButton extends Composite{

	int min=32;

	int max=20000;
	public BitrateChangeButton(Composite parent,int changeValue,ParamItem target) {
		super(parent, SWT.NULL);
		this.setLayout(LayoutBuilder.makeGridLayoutNoMargin(2, false));
		Button plus=new Button(this,SWT.NULL);
		plus.setText("+");
		Button minus=new Button(this,SWT.NULL);
		minus.setText("-");
		plus.addSelectionListener(new ParamItemValueChanger(target,changeValue));
		minus.addSelectionListener(new ParamItemValueChanger(target,-changeValue));
	}
	
	public class ParamItemValueChanger implements SelectionListener{
		ParamItem target;
		String prefix="k";
		int change;
		public ParamItemValueChanger(ParamItem target,int change){
			this.target=target;
			this.change=change;
		}
		@Override
		public void widgetDefaultSelected(SelectionEvent e) {

		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			String value=target.getValue();
			if(value.endsWith(prefix)){
				int v=NumberUtils.toInt(value.substring(0,value.length()-prefix.length()), 0);
				v+=change;
				if(v<min){
					v=min;
				}
				if(v>max){
					v=max;
				}
				target.setValue(v+prefix);
			}
		}
		
	}
	

}
